package com.ochnas.volunteersignup.ui;

import static com.ochnas.volunteersignup.ui.SharedConstants.PASSWORD_PATTERN;

import com.ochnas.volunteersignup.domain.User;
import com.ochnas.volunteersignup.service.UserService;

public class ChangePasswordController {
	
	private ApplicationService applicationService;
	private UserService userService;
	private User user;
	
	private String currentPassword = ""; 
	private String newPassword = "";
	private String confirmPassword = "";
	private String dbPassword = "";
	
	public ChangePasswordController() {
		super();
		user = new User();
	}
	
	public String changePassword () {
		String userId = applicationService.getRequestParameter("userId");
		try {
			user = userService.findUserAccount(Long.parseLong(userId));
		} catch (NumberFormatException e) {
			return null;
		}
		dbPassword = user.getPassword();
		
		return "changePassword";
	}
	
	public String cancel() {
		user = new User();
		return "manageAccount";
	}
		
	public String savePassword () {
		
		boolean isValid = true;
		boolean passwordsOkay = true;
					
		// validate if password fields
		if(currentPassword == null || "".equals(currentPassword)){
			   applicationService.addErrorMessage("changePasswordForm:oldPassword","Please enter a password.");
			   isValid = false;
			   passwordsOkay = false;
		} else {
			if(!PASSWORD_PATTERN.matcher(currentPassword).matches()) {
				applicationService.addErrorMessage("changePasswordForm:oldPassword",
				"Password lenght must be between 6 to 12 alphanumeric, including special characters.");
				   isValid = false;
				   passwordsOkay = false;
			} 
		}
		
		// Check to see if Password matches with DBC
		if(!dbPassword.equals(currentPassword)) {
			applicationService.addErrorMessage("changePasswordForm:oldPassword",
			"Current password does not match database password.  \nPlease re-enter current Password.");
			   isValid = false;
			   passwordsOkay = false;
		}
				
		if(newPassword == null || "".equals(newPassword)){
			   applicationService.addErrorMessage("changePasswordForm:newPWD","Please enter a password.");
			   isValid = false;
			   passwordsOkay = false;
		} else {
			if(!PASSWORD_PATTERN.matcher(newPassword).matches()) {
				applicationService.addErrorMessage("changePasswordForm:newPWD",
				"Password lenght must be between 6 to 12 alphanumeric, including special characters.");
				   isValid = false;
				   passwordsOkay = false;
			}
		}
		
		if(confirmPassword == null || "".equals(confirmPassword)){
			   applicationService.addErrorMessage("changePasswordForm:confirmPWD","Please enter a password.");
			   isValid = false;
			   passwordsOkay = false;
		} else {
			if(!PASSWORD_PATTERN.matcher(confirmPassword).matches()) {
				applicationService.addErrorMessage("changePasswordForm:confirmPWD",
				"Password lenght must be between 6 to 12 alphanumeric, including special characters.");
				   isValid = false;
				   passwordsOkay = false;
			}
		}
				
		// validate if new passwords entered match
		if (passwordsOkay) {
			if(!newPassword.equals(confirmPassword)) {
				applicationService.addErrorMessage("changePasswordForm:newPWD",
				"Passwords do not match.");
				applicationService.addErrorMessage("changePasswordForm:confirmPWD",
				"Passwords do not match.");
				   isValid = false;
				   newPassword = "";
				   confirmPassword = "";
			}
		}
				
	   	if (isValid)  {
	   		try {
	   			user.setPassword(newPassword);
	   			user = userService.updatePassword(user.getId(), user.getPassword());
	   		} catch (Exception e) {
					applicationService.addErrorMessage("changePasswordForm:summary", e.getMessage());
					return null;
			   }
		    } else {
				return null;
			}
	   	
	   	user = new User();
		applicationService.addErrorMessage("manageAccountForm:summary", "Password change was successfully.");
		return "manageAccount";
	   	
	} // end of edit user account method

	public ApplicationService getApplicationService() {
		return applicationService;
	}

	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getCurrentPassword() {
		return currentPassword;
	}

	public void setCurrentPassword(String currentPassword) {
		this.currentPassword = currentPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}
		
}
